//package cn.xie.blog.servergateway.handler;
//
//import cn.xie.blog.model.common.enums.response.ResponseCodeEnum;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import jakarta.annotation.Resource;
//import jakarta.servlet.ServletException;
//import jakarta.servlet.ServletOutputStream;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.security.authentication.BadCredentialsException;
//import org.springframework.security.authentication.DisabledException;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.web.authentication.AuthenticationFailureHandler;
//import org.springframework.stereotype.Component;
//
//import java.io.IOException;
//import java.nio.charset.StandardCharsets;
//
///**
// * @description: 登录失败处理器
// * @author: xiaoxie
// * @date: 2025/4/30 上午10:59
// **/
//@Component
//public class LoginFiledHandler implements AuthenticationFailureHandler {
//
//    private final Logger logger = LoggerFactory.getLogger(LoginFiledHandler.class);
//    @Resource
//    private ObjectMapper objectMapper;
//
//    @Override
//    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
//        logger.info("用户登录失败：{}", request.getParameter("username"));
//        logger.debug("请求URL：{}", request.getRequestURI());
//        logger.debug("异常：{}", exception.getMessage());
//
//        response.setContentType("application/json;charset=UTF-8");
//        String str = "";
//        if (exception instanceof BadCredentialsException) {
//            str = "用户名或密码错误，登录失败!";
//            logger.debug("提供的凭据不正确");
//        }
//        if (exception instanceof DisabledException) {
//            str = "账户被禁用，登录失败!";
//            logger.debug("账户已被禁用");
//        }
//
//        ServletOutputStream out = response.getOutputStream();
//        out.write(objectMapper.writeValueAsString(ResponseCodeEnum.ACCOUNT_OR_PASSWORD_ERROR).getBytes(StandardCharsets.UTF_8));
//        out.flush();
//        out.close();
//    }
//}